{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 原文地址 [blog.csdn.net](https://blog.csdn.net/yoloyy/article/details/61195570)\n",
    "\n",
    "文章分类对应标签数据库设计\n",
    "=============\n",
    "\n",
    "> 几乎在大部分有管新闻列表项目中，都涉及文章与标签的设计，应该说比较常见、典型的例子。考虑了很多种办法，见识了三四种对应的关系，现在我来说一种最节省数据库资源的文章分类与标签数据库设计的办法\n",
    "\n",
    "### 需求说明\n",
    "\n",
    "*   新闻分类，标签说明\n",
    "*   详细需求  \n",
    "    ![](https://imgconvert.csdnimg.cn/aHR0cDovL2ltYWdlczIwMTUuY25ibG9ncy5jb20vYmxvZy8xMTEyNzc1LzIwMTcwMi8xMTEyNzc1LTIwMTcwMjI1MTEzNzM2MzUxLTEwNDg5NTU4ODkucG5n?x-oss-process=image/format,png)\n",
    "\n",
    "### 数据表设计\n",
    "\n",
    "> 在这里我们要建三张表，Acticle 文章信息表，Tags 标签表 和 ActMappTags 映射表\n",
    "\n",
    "1、Acticle 文章信息表\n",
    "\n",
    "<table><thead><tr><th>id</th><th>title</th><th>description</th></tr></thead><tbody><tr><td>1</td><td>重大利好消息来袭，光伏行业有望打破坚冰</td><td>光伏产业潜伏盘整已有较长一段时间，最新的几项利好消息或许能打破坚冰，促进产业回暖。</td></tr><tr><td>2</td><td>华谊兄弟 5 年来首次收入下滑 如何突破瓶颈还是未知数</td><td>2 月 23 日晚间，华谊兄弟发布了 2016 年年报。年报显示，报告期公司实现营业总收入为 34.73 亿元，较上年同期下滑 10.34%，归属于上市公司股东净利润为 8.08 亿元，较上年同…</td></tr></tbody></table>\n",
    "\n",
    "2、tags 标签表\n",
    "\n",
    "<table><thead><tr><th>id</th><th>tag</th></tr></thead><tbody><tr><td>1</td><td>能化</td></tr><tr><td>2</td><td>股市</td></tr><tr><td>3</td><td>经济</td></tr><tr><td>4</td><td>公司</td></tr><tr><td>5</td><td>要闻</td></tr></tbody></table>\n",
    "\n",
    "3、ActMappTags 映射表(中间表）\n",
    "\n",
    "<table><thead><tr><th>id</th><th>a_id</th><th>t_id</th></tr></thead><tbody><tr><td>1</td><td>1</td><td>1</td></tr><tr><td>2</td><td>1</td><td>2</td></tr><tr><td>3</td><td>1</td><td>3</td></tr><tr><td>4</td><td>2</td><td>4</td></tr><tr><td>5</td><td>2</td><td>5</td></tr></tbody></table>\n",
    "\n",
    "4 、表与表之间的关系图  \n",
    "![](https://imgconvert.csdnimg.cn/aHR0cDovL2ltYWdlczIwMTUuY25ibG9ncy5jb20vYmxvZy8xMTEyNzc1LzIwMTcwMi8xMTEyNzc1LTIwMTcwMjI1MTEzNzUzNDkxLTE3NTE1MDA4NS5qcGc?x-oss-process=image/format,png)\n",
    "\n",
    "### 怎么查询遍历出数据\n",
    "\n",
    "首先，我们来建表三张表。\n",
    "\n",
    "```\n",
    "CREATE TABLE acticle(\n",
    "  id IfNT UNSIGNED PRIMARY KEY AUTO_INCREMENT,\n",
    "  title VARCHAR(120) NOT NULL DEFAULT '' COMMENT '文章标题',\n",
    "  description VARCHAR(255) NOT NULL DEFAULT '' COMMENT '信息描述'\n",
    ")ENGINE MyISAM CHARSET UTF8 COMMENT '文章表';\n",
    "\n",
    "INSERT INTO acticle (title, description) VALUES ('重大利好消息来袭，光伏行业有望打破坚冰','光伏产业潜伏盘整已有较长一段时间，最新的几项利好消息或许能打破坚冰，促进产业回暖。'),('华谊兄弟5年来首次收入下滑 如何突破瓶颈还是未知数','2月23日晚间，华谊兄弟发布了2016年年报。年报显示，报告期公司实现营业总收入为34.73亿元，较上年同期下滑10.34%，归属于上市公司股东净利润为8.08亿元，较上年同...');\n",
    "\n",
    "CREATE TABLE tags(\n",
    "  id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,\n",
    "  tag_name VARCHAR(56) NOT NULL DEFAULT '' COMMENT '标签名字'\n",
    ")ENGINE MyISAM CHARSET UTF8 COMMENT '标签表';\n",
    "\n",
    "INSERT INTO tags (tag_name) VALUES ('能化'),('股市'),('经济'),('公司'),('要闻');\n",
    "\n",
    "CREATE TABLE ActMappTags(\n",
    "  id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,\n",
    "  a_id INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'acticle的ID',\n",
    "  t_id INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'tags的ID'\n",
    ")ENGINE MyISAM CHARSET UTF8 COMMENT '映射表';\n",
    "\n",
    "INSERT INTO ActMappTags (a_id, t_id) VALUES (1,1),(1,2),(1,3),(2,4),(2,5);\n",
    "\n",
    "\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
